package com.situ.web.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletContext servletContext = getServletContext();
		//1. a.txt
		//Servlet是运行在服务器端的代码，"/"代表的是部署到tomcat服务器后Java812Web这个目录
		String aPath = servletContext.getRealPath("/WEB-INF/classes/a.txt");
		//aPath:C:\apache-tomcat-7.0.52\webapps\Java1812Web\WEB-INF\classes\a.txt
		System.out.println("aPath:" + aPath);
		//2. b.txt
		String bPath = servletContext.getRealPath("/b.txt");
		//bPath:C:\apache-tomcat-7.0.52\webapps\Java1812Web\b.txt
		System.out.println("bPath:" + bPath);
		//3. c.txt
		String cPath = servletContext.getRealPath("/WEB-INF/c.txt");
		//cPathC:\apache-tomcat-7.0.52\webapps\Java1812Web\WEB-INF\c.txt
		System.out.println("cPath" + cPath);
		//4. d.txt 没有部署到tomcat服务器，所以没法获取路径
		//String dPath = servletContext.getRealPath("d.txt");
		//System.out.println("dPath" + dPath);
	}
}
